﻿using B.S.Data.MES.API.Applications.QualityManagement.ExamineDesignCommand.Commands;
using B.S.Domain.BasicData.QualityManagement;
using B.S.Infratrctrue;
using B.S.Requst;
using MediatR;
using System.Collections;

namespace B.S.Data.MES.API.Applications.QualityManagement.ExamineDesignCommand.CommandHandlers
{
    public class AllDeleteExamineDesignCommandHandler : IRequestHandler<AllDeleteExamineDesignCommand, APIRequst<int>>
    {
        private readonly IBaseRepository<ExamineDesign> _examinedesignRepository;
        public AllDeleteExamineDesignCommandHandler(IBaseRepository<ExamineDesign> examinedesignRepository)
        {
            _examinedesignRepository = examinedesignRepository;
        }
        public async Task<APIRequst<int>> Handle(AllDeleteExamineDesignCommand request, CancellationToken cancellationToken)
        {
            APIRequst<int> result = new APIRequst<int>();
            var codearray = request.designs.Split(',').Select(x => Convert.ToInt32(x)).ToList();
            if (codearray == null)
            {
                result.Code = RequstCode.失败;
                result.Msg = RequstCode.失败.ToString();
                return await Task.FromResult(result);
            }

            var code = _examinedesignRepository.GetAll().Where(x => codearray.Contains(x.DesignId)).ToList();
            foreach (var item in code)
            {
                item.isDel = true;
            }
            var ment = await _examinedesignRepository.UpdateRange(code);
            if(ment>0)
            {
                result.Code = RequstCode.成功;
                result.Msg = RequstCode.成功.ToString();

            }
            return await Task.FromResult(result);

        }
    }
}
